﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Text.RegularExpressions;
using Estimate.Common.Estimate.EstimateValue;
using Estimate.Common.Repository;
using Estimate.Common.DBFieldTypes;
using Estimate.Common.Entity;

namespace Estimate.Common.Estimate
{
    public class WaterPrediction
    {
        /// <summary>
        /// 预报站对应的实况站的对应字典
        /// </summary>
        protected Dictionary<string, List<string>> _realStation4ForcastStation;

        /// <summary>
        /// 以预报站的站号为key，存放预报情况
        /// </summary>
        protected Dictionary<string, WaterForecast> _forecastList;

        private string _connectionString;

        private RealStationCompareType _compareType = RealStationCompareType.None;
        /// <summary>
        /// 预报站与实况站对照关系
        /// </summary>
        public RealStationCompareType CompareType
        {
            get { return _compareType; }
            set
            {
                _compareType = value;
                ResetRealStations();
            }
        }

        /// <summary>
        /// 将存储的处理好的预报结果字符串化
        /// 或由字符串初始化 Forecast 类
        /// </summary>
        public string SemiResult
        {
            get
            {
                List<string> semiResult = new List<string>(_forecastList.Count);
                foreach (WaterForecast forecast in _forecastList.Values)
                {
                    semiResult.Add(forecast.SemiResult);
                }
                return string.Join("|", semiResult.ToArray());
            }

            
        }

        public WaterPrediction(string connectionString, RealStationCompareType type)
        {
            _connectionString = connectionString;
            _forecastList = new Dictionary<string, WaterForecast>();
            CompareType = type;         
               
        }

        private void ResetRealStations()
        {
            RealStationCompareRepository repository = new RealStationCompareRepository(_connectionString);
            ForeRealStationCompareEntity[] entitys = repository.GetAllRecord(_compareType);

            _realStation4ForcastStation = new Dictionary<string, List<string>>();
            _forecastList = new Dictionary<string, WaterForecast>();

            foreach (ForeRealStationCompareEntity entity in entitys)
            {
                if(! _realStation4ForcastStation.ContainsKey(entity.RealStation))
                {
                    _realStation4ForcastStation[entity.RealStation] = new List<string>();
                }
                _realStation4ForcastStation[entity.RealStation].Add(entity.ForeStation);
                WaterForecast forecast = new WaterForecast();
                forecast.FStNum = entity.ForeStation ;
                _forecastList[entity.ForeStation] = forecast;
            }            
        }
        

        /// <summary>
        /// 输入预报站及对应的预报值
        /// </summary>
        /// <param name="value">站号;预报值| 的字符串</param>
        public void InputPrediction(string value)
        {
            if (value.Trim() == "")
                return;
            string pattern = @"\|";
            string[] forserial = Regex.Split(value, pattern, RegexOptions.IgnoreCase);
            for (int i = 0; i < forserial.Length; i++)
            {
                string[] oserial = Regex.Split(forserial[i], @"\;", RegexOptions.IgnoreCase);

                string fstnum = oserial[0];
                if (_forecastList.ContainsKey(fstnum))
                {
                    _forecastList[fstnum].FStNum = fstnum;
                    float result;
                    float.TryParse(oserial[1].ToString(), out result);                    
                    _forecastList[fstnum].FValue = result;
                }
            }
        }

        /// <summary>
        /// 输入实况站的值
        /// </summary>
        /// <param name="value">站号;观测值| 的字符串</param>
        public void InputReal(string value, ForeSemiType semitype)
        {
            if (value == "")
                return;
            string[] realserial = Regex.Split(value, @"\|", RegexOptions.IgnoreCase);

            for (int i = 0; i < realserial.Length; i++)
            {
                if (realserial[i] == "")
                    continue;
                string[] oserial = Regex.Split(realserial[i], @"\;", RegexOptions.IgnoreCase);

                string rstnum = oserial[0];
                float result;
                float.TryParse(oserial[1].ToString(), out result);

                if (_realStation4ForcastStation.ContainsKey(rstnum))
                {
                    foreach (string fstnum in _realStation4ForcastStation[rstnum])
                    {
                        if (_forecastList.ContainsKey(fstnum))
                        {
                            _forecastList[fstnum].SemiType = semitype;
                            if (oserial[1] == "null")
                            {//只有人工站存在 null 的情况。因此设置为-1
                                _forecastList[fstnum].RStNum = rstnum;
                                _forecastList[fstnum].RValue = -1f;                                
                            }
                            else
                            {
                                _forecastList[fstnum].RStNum = rstnum;
                                _forecastList[fstnum].RValue = result;
                            }
                        }
                    }                    
                }                              
            }
        }

        /// <summary>
        /// 根据指定的时间以及降水类型，计算相应的值
        /// </summary>
        /// <param name="span">时间间隔</param>
        /// <param name="waterKind">降水类型</param>
        public void PreDealRainReal(EstimateHourSpan span, WaterfallKind waterKind)
        {
            foreach (WaterForecast forecast in _forecastList.Values)
            {
                PreDealSingleForecaster(forecast, span, waterKind);                
            }
        }


        public static void  PreDealSingleForecaster(WaterForecast forecast, EstimateHourSpan span, WaterfallKind waterKind)
        {
            if (forecast.FValue >= 21 && forecast.FValue <= 28) //预报跨量级
            {
                //跨量级降水
                /* 2014 -04 -24 徐淮杰
                 * 跨量级降水，改用对应的大量级降水进行预报
                 
                MultiPrecipitationLevel level = Calculate.RealValue2MultiPrecipitation(span, waterKind, forecast.RValue, forecast.SemiType);
                forecast.PrecipitationLevelInspection = Calculate.MultiPrecipitationLevelInspection(level, (int)forecast.FValue);
                 */
                PrecipitationLevel level = Calculate.RealValue2PrecipitationLevel(span, waterKind, forecast.RValue, forecast.SemiType);
                int FValue = (int)forecast.FValue;
                
                switch (FValue)
                {
                    case 21: //小到中雨按中雨评
                        FValue = 8;
                        break;
                    case 22: //中到大雨按大雨评
                        FValue = 9;
                        break;
                    case 23: //大到暴雨按暴雨评
                        FValue = 10;
                        break;
                    case 24: //暴雨到大暴雨按大暴雨评
                        FValue = 11;
                        break;
                    case 25: //大暴雨到特大暴雨按特大暴大雨评
                        FValue = 12;
                        break;
                    case 26: //小到中雪按中雪评
                        FValue = 15;
                        break;
                    case 27: //中到大雪按大雪评
                        FValue = 16;
                        break;
                    case 28: //大到暴雪按暴雪评
                        FValue = 17;
                        break;
                }
                forecast.PrecipitationLevelInspection = Calculate.PrecipitationLevelInspection(level, FValue);
            }
            else
            {
                //分级降水
                PrecipitationLevel level = Calculate.RealValue2PrecipitationLevel(span, waterKind, forecast.RValue,forecast.SemiType);
                forecast.PrecipitationLevelInspection = Calculate.PrecipitationLevelInspection(level, (int)forecast.FValue);
            }
            //一般降水
           
            WaterfallLevel waterLevel = Calculate.RealValue2WaterfallLevel(span, waterKind, forecast.RValue,forecast.SemiType);
            forecast.WaterfallLevelInspection = Calculate.WaterfallLevelInspection(waterLevel, (int)forecast.FValue);

            //晴雨
            WaterfallAmount amount = Calculate.RealValue2WaterfallAmount(forecast.RValue, forecast.SemiType);
            forecast.WaterfallAmountInspection = Calculate.WaterfallAmountInspection(amount, (int)forecast.FValue);
            //累积降水
            SumPrecipitationLevel sumLevel = Calculate.RealValue2SumPrecipitationLevel(forecast.RValue, forecast.SemiType);
            forecast.SumPrecipitationLevelInspection = Calculate.SumPrecipitationLevelInspection(sumLevel, (int)forecast.FValue);
            
        }
    }
}
